package com.xunwulian.web.controller;

import com.google.gson.Gson;
import com.xunwulian.base.bean.CountSmsByDateEntity;
import com.xunwulian.base.entity.User;
import com.xunwulian.web.entity.BillEntityWrapper;
import com.xunwulian.web.util.Const;
import org.apache.commons.lang.time.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liyankai on 2017/3/30.
 */
@Controller
@RequestMapping(value = "/main")
public class MainController extends BaseController {

    // 显示最近账单条数
    private static final Integer TOP_BILL_NUM = 6;

    // 最新天数
    private static final Integer TOP_DAYS_NUM = 7;

    // TOP10
    private static final Integer TOP_NUM = 10;

    Gson gson = new Gson();

    /**
     * 首页账单Top6
     *
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/topBill")
    @ResponseBody
    public Object topBill() throws IOException {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        List<BillEntityWrapper> wrappers = new ArrayList();

        return gson.toJson(wrappers);
    }

    /**
     * 短信TOP
     *
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/topSms")
    @ResponseBody
    public Object topSms() throws IOException {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();

        User user = (User) session.getAttribute(Const.SESSION_USER);

        Date startDate = DateUtils.ceiling(DateUtils.addDays(new Date(), -TOP_NUM),
                DateFormat.Field.DAY_OF_MONTH.getCalendarField());

        logger.info("startDate:" + startDate);

        return gson.toJson(null);
    }


    /**
     * 按天显示短信条数
     *
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/smsByDay")
    @ResponseBody
    public Object smsByDay() throws IOException {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        List<CountSmsByDateEntity> result = new ArrayList();

        Date startDate = DateUtils.ceiling(DateUtils.addDays(new Date(), -TOP_DAYS_NUM),
                DateFormat.Field.DAY_OF_MONTH.getCalendarField());

        logger.info("startDate:" + startDate);

        return gson.toJson(result);
    }

    /**
     * 流量TOP
     *
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/topGprs")
    @ResponseBody
    public Object topGprs() throws IOException {

        // TODO 实现逻辑

        return null;
    }

    /**
     * 按天显示流量
     *
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/gprsByDay")
    @ResponseBody
    public Object gprsByDay() throws IOException {

        // TODO 实现逻辑

        return null;
    }


    /**
     * 卡状态
     *
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/cardInfoStatus")
    @ResponseBody
    public Object cardInfoStatus() throws IOException {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        return gson.toJson(null);
    }

    /**
     * 按天显示充值
     *
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/transByDay")
    @ResponseBody
    public Object transByDay() throws IOException {

        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);


        return gson.toJson(null);
    }


}
